package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.EnterpriseStatusEnum;
import com.ruoyi.common.enums.PositionStatusEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysEnterprise;
import com.ruoyi.system.service.ISysEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysPositionMapper;
import com.ruoyi.system.domain.SysPosition;
import com.ruoyi.system.service.ISysPositionService;

/**
 * 岗位Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-09
 */
@Service
public class SysPositionServiceImpl extends ServiceImpl<SysPositionMapper ,SysPosition> implements ISysPositionService
{
    @Autowired
    private SysPositionMapper sysPositionMapper;

    @Autowired
    private ISysEnterpriseService enterpriseService ;

    /**
     * 查询岗位
     * 
     * @param id 岗位主键
     * @return 岗位
     */
    @Override
    public SysPosition selectSysPositionById(Long id)
    {
        return sysPositionMapper.selectSysPositionById(id);
    }

    /**
     * 查询岗位列表
     * 
     * @param sysPosition 岗位
     * @return 岗位
     */
    @Override
    public List<SysPosition> selectSysPositionList(SysPosition sysPosition)
    {
        List<SysPosition> list = sysPositionMapper.selectSysPositionList(sysPosition);
        for (SysPosition position : list) {
            position.setEnterprise(enterpriseService.getById(position.getEnterpriseId())) ;
        }
        return list;
    }

    /**
     * 新增岗位
     * 
     * @param sysPosition 岗位
     * @return 结果
     */
    @Override
    public int insertSysPosition(SysPosition sysPosition)
    {
        SysEnterprise enterprise = enterpriseService.getById(sysPosition.getEnterpriseId());
        if (ObjectUtils.isEmpty(enterprise)) {
            throw new ServiceException("企业信息不存在!") ;
        }
        String status = enterprise.getStatus();
        if (!EnterpriseStatusEnum.ENTERPRISE_ONLINE.getCode().equals(status)) {
            throw new ServiceException("企业状态为:" + EnterpriseStatusEnum.getStatusName(status) + "，不能新增岗位!") ;
        }
        sysPosition.setEnterpriseName(enterprise.getName());
        sysPosition.setCreateTime(DateUtils.getNowDate());
        sysPosition.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        sysPosition.setStatus(PositionStatusEnum.POSITION_ONLINE.getCode());
        return sysPositionMapper.insert(sysPosition);
    }

    /**
     * 修改岗位
     * 
     * @param sysPosition 岗位
     * @return 结果
     */
    @Override
    public int updateSysPosition(SysPosition sysPosition)
    {
        sysPosition.setUpdateTime(DateUtils.getNowDate());
        return sysPositionMapper.updateSysPosition(sysPosition);
    }

    /**
     * 批量删除岗位
     * 
     * @param ids 需要删除的岗位主键
     * @return 结果
     */
    @Override
    public int deleteSysPositionByIds(Long[] ids)
    {
        return sysPositionMapper.deleteSysPositionByIds(ids);
    }

    /**
     * 删除岗位信息
     * 
     * @param id 岗位主键
     * @return 结果
     */
    @Override
    public int deleteSysPositionById(Long id)
    {
        return sysPositionMapper.deleteSysPositionById(id);
    }

    @Override
    public List<SysPosition> enterpriseList(SysPosition sysPosition) {
        return sysPositionMapper.enterpriseList(sysPosition);
    }

    @Override
    public boolean changePositionStatus(SysPosition sysPosition) {
        if (ObjectUtils.isEmpty(sysPosition)) {
            throw new ServiceException("岗位信息不能为空!" ) ;
        }
        if (ObjectUtils.isEmpty(sysPosition.getId())) {
            throw new ServiceException("岗位编号不能为空!") ;
        }
        if (StringUtils.isBlank(sysPosition.getStatus())) {
            throw new ServiceException("岗位状态不能为空!") ;
        }
        LambdaUpdateWrapper<SysPosition> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SysPosition::getId ,sysPosition.getId()) ;
        wrapper.set(SysPosition::getStatus ,sysPosition.getStatus()) ;
        boolean update = this.update(wrapper);
        return update;
    }
}
